package oa.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.opensymphony.xwork2.ActionContext;
import oa.dao.FormDao;
import oa.domain.workflow.Approve;
import oa.domain.workflow.Form;
import oa.service.FormService;
import oa.util.OAUtils;
@Service("formService")
public class FormServiceImpl implements FormService {

	@Resource(name="formDao")
	private FormDao formDao  ;
	@Override
	public Collection<Form> getAllForm() {
		// TODO Auto-generated method stub
		return this.formDao.getAllEntry();
	}

	@Override
	public Form getFormById(Long fid) {
		// TODO Auto-generated method stub
		return this.formDao.getEntryById(fid);
	}
    /**
     * 下载表单信息
     */
	@Override
	public InputStream download(Long fid) throws Exception {
		// TODO Auto-generated method stub
		Form form = this.formDao.getEntryById(fid);
		String url = form.getUrl();
		String fileName = form.getTitle();
		fileName = URLEncoder.encode(fileName, "utf-8");
		ActionContext.getContext().put("fileName", fileName);
		return new FileInputStream(new File(url));
	}
	
    /**
     * 我的申请查询列表
     */
	@Override
	public Collection<Form> myApplicationList() {
		// TODO Auto-generated method stub
		String username = OAUtils.getUserFromSession().getUsername();
		Collection<Form> formList = this.formDao.getFormByApplicator(username);
		return formList;
	}

	@Override
	public List<Approve> getApprovesById(Long fid) {
		// TODO Auto-generated method stub
		Form form = this.formDao.getEntryById(fid);
		Set<Approve> approves = form.getApproves();
		return new ArrayList<Approve>(approves);
	}

}
